package com.litchi.tutor.controller;

import cn.hutool.http.server.HttpServerRequest;
import com.litchi.tutor.common.base.ResponseVO;
import com.litchi.tutor.common.entity.User;
import com.litchi.tutor.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/user")
@Api("用户管理")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "/loginByCode",method = RequestMethod.POST)
    @ApiOperation("根据code登录")
    public ResponseVO loginByCode(@RequestParam("code") String code) throws Exception {
        log.info(code);
        return ResponseVO.ok(userService.loginByCode(code));
    }

    @RequestMapping(value = "/loginByOpenId", method = RequestMethod.POST)
    @ApiOperation("根据openId登录")
    public ResponseVO loginByOpenId(String openId) {
        log.info(openId);
        return ResponseVO.ok(userService.loginByOpenId(openId));
    }

    @RequestMapping(value = "/userinfo", method = RequestMethod.GET)
    @ApiOperation("查询用户信息，传了就查相应用户的信息，否则查自己的")
    public ResponseVO getUserInfo(@RequestParam(required = false) String userid, HttpServletRequest request) {
        User user = userService.getUserInfo(userid, request);
        return ResponseVO.ok(user);
    }

    @RequestMapping(value = "/role", method = RequestMethod.PUT)
    @ApiOperation("用户设置角色,role（必填）传 1 表示注册为家长，2 表示注册为老师")
    public ResponseVO updateRole(@RequestParam Integer role, HttpServletRequest request) {
        userService.updateRole(role,request);
        return ResponseVO.ok(null);
    }



}
